<template>
	<view class="wrap">
		<view class="key-input">
			<view class="title">输入验证码</view>
			<view class="tips">验证码已发送至 +86 {{$userInfo.mobile}}</view>
				<u-code-input
					v-model="code"
					mode="line"
					size="30"
					:maxlength="4"
					hairline
					color="#3c9cff"
					borderColor="#3c9cff"
					
					@finish="finishCode"
				></u-code-input>


		 
		</view>
		<text v-if="errCode" :class="{ error: error }">验证码错误，请重新输入</text>
		<view class="captcha">
			<text  v-if="getCode" style="color: #7468BE;" @tap="noCaptcha">收不到验证码?点这里。</text>
			<text  v-else >{{ second }}秒后重新获取验证码</text>
		</view>
	</view>
</template>

<script>
var that
 import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
 let UserInfo = require('../../../../service/UserInfo') , mUserInfo;
export default {
	data() {
		return {
			mobile:'',
			getCode:true,
			errCode:false,
			code:'',
			maxlength: 4,
			value: '',
			second: 60,
			show: false,
			error: false,
			account:{},
			isLoginPage : true
			
		};
	},
	computed: {
		...mapState('login',['$userInfo'])
	},
	onLoad( ) {
		 that = this 
		 
		 mUserInfo = new UserInfo(this.mNetTool)
		let interval = setInterval(() => {
			this.second--;
			if (this.second <= 0) {
				this.getCode = true;
				if (this.value.lenth != 4) {
					this.error = true;
				}
				clearInterval(interval);
			}
		}, 1000);
	},
 
	methods: {
		...mapMutations('login',{setUserInfo:'SET_USERINFO'}),
		finishCode(){ 
			this.mNetTool.getAjaxUrlValueBackground('capi/loomoSMS/checkCode',{
					mobile:that.$userInfo.mobile,
					code:that.code
			},res => { 
				if(res.success){
					mUserInfo.save({
						mobile:that.$userInfo.mobile,
						password:that.$userInfo.password,
						type:3,
						real_name:'新用户'
					},res =>{
						that.setUserInfo(res.rows)
						uni.setStorageSync('userInfo',userInfo)
						uni.switchTab({
							url:'../../home/home'
						})
					})
				}else {
					uni.$u.toast('验证码错误')
					uni.hideLoading()
				}
			})
			uni.$u.toast('检测到验证码已经输入')
		},
		// 收不到验证码选择时的选择
		noCaptcha() { 
			uni.showActionSheet({
				itemList: ['重新获取验证码'],
				success: (res)=> {
				 this.mNetTool.getAjaxUrlValueBackground('capi/loomoSMS/getCode',{
				 		mobile:that.$userInfo.mobile,
				 		signature:'科目叁'
				 },res => {
				 	console.log(res)
				 	if(res.success){
						this.getCode = false
				 		this.$refs.uCode.start();
				 		uni.$u.toast('验证码发送成功请，注意查收')
				 	 
				 	}else{
				 		uni.$u.toast('服务器错误！')
				 	}
				 })
				},
				fail: function(res) {
					
				}
			});
		},
		// change事件侦听
		change(value) {
			// console.log('change', value);
		},
		// 输入完验证码最后一位执行
		finish(value) {
			// console.log('finish', value);
		}
	}
};
</script>

<style lang="scss" scoped>
page{
	background-color: #fff;
}
.wrap {
	padding: 80rpx;
} 
.key-input {
	width: 500rpx; 
	margin: 30rpx auto;
	padding: 30rpx 0;
	text {
		display: none;
	}
	.error {
		display: block;
		color: red;
		font-size: 30rpx;
		margin: 20rpx 0;
	}
}

.title {
	font-size: 50rpx;
	color: #333;
}

.key-input .tips {
	font-size: 30rpx;
	color: #333;
	margin-top: 20rpx;
	margin-bottom: 60rpx;
}
.captcha {
	// color: $u-type-warning;
	font-size: 30rpx;
	margin-top: 40rpx;
	.noCaptcha {
		display: block;
	}
	.regain {
		display: block;
	}
}
</style>
